<!--
@Description:商品自动下架配置
@Author:zqy
@Date:2024-03-05 10:47:19
@Last Modified by:zqy
@Last Modified time:2024-03-05 10:47:19
-->
<template>
  <el-dialog
    ref="refDlg"
    v-el-drag-dialog
    width="700px"
    title="自动下架状态配置"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @close="closeDialog"
  >
    <div>
      <div style="color:red;text-align:center;margin-bottom:20px">注意：系统将定时对该楼层商品符合以下勾选状态产品进行下架</div>
      <el-form ref="form" :model="formData" size="mini" label-width="140px">
        <!-- :rules="formRules" -->
        <el-form-item label="在产状态：" prop="productState">
          <QupDictCheckbox
            v-model="formData.productState"
            dict-type-code="PRODUCT_STATE"
            domain-code="marketing"
            app-code="sad"
          />
        </el-form-item>
        <el-form-item label="采购控制状态：" prop="purchaseHandle">
          <QupDictCheckbox
            v-model="formData.purchaseHandle"
            dict-type-code="PURCHASE_HANDLE"
            domain-code="marketing"
            app-code="sad"
          />
        </el-form-item>
        <el-form-item label="启用状态：" prop="enableFlag">
          <el-checkbox-group v-model="formData.enableFlag">
            <el-checkbox label="1">启用</el-checkbox>
            <el-checkbox label="0">停用</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="closeDialog">取 消</el-button>
      <el-button v-loading="submiting" type="primary" size="mini" @click="handleSure">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { queryProductAutoStatusConfig, saveProductAutoStatusConfig } from '@/api/floorProductDetail'
export default {
  name: 'PermissionRoleList',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    floorId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      submiting: false,
      formData: {
        productState: [],
        purchaseHandle: [],
        enableFlag: []
      },
      formRules: {
        productState: [
          { required: true, message: '请选择在产状态', trigger: 'change' }
        ],
        purchaseHandle: [
          { required: true, message: '请选择采购状态', trigger: 'change' }
        ],
        enableFlag: [
          { required: true, message: '请选择停启用状态', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    queryProductAutoStatusConfig(this.floorId).then(res => {
      if (res.data && res.data.id) {
        const d = res.data
        this.$set(this.formData, 'id', d.id)
        this.$set(this.formData, 'productState', d.productState.split(','))
        this.$set(this.formData, 'purchaseHandle', d.purchaseHandle.split(','))
        this.$set(this.formData, 'enableFlag', d.enableFlag.split(','))
      }
    })
  },
  methods: {
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        productState: [],
        purchaseHandle: [],
        enableFlag: []
      }
      this.$refs.form && this.$refs.form.clearValidate()
      done && done()
    },
    handleSure() {
      this.submiting = true
      const { enableFlag, productState, purchaseHandle, id } = this.formData
      const params = {
        enableFlag: enableFlag.join(','),
        productState: productState.join(','),
        purchaseHandle: purchaseHandle.join(','),
        v2FloorConfId: this.floorId
      }
      if (id) {
        params.id = id
      }
      saveProductAutoStatusConfig(params).then(res => {
        this.submiting = false
        if (res.success === 1) {
          this.$message.success(`${params.id ? '编辑' : '新增'}保存成功`)
          this.closeDialog()
        }
      }).catch(() => {
        this.submiting = false
      })
      if (this.submiting) return
      /* this.$refs['form'].validate(async(valid) => {
        if (valid) {

        }
      }) */
    }
  }
}
</script>

<style lang='scss' scope >
</style>
